<?php

class Stock extends Model {

	private $insertStatement;
	private $listPackageTypeIdsStatement;

	function __construct(){
		parent::__construct();
		//$this->insertStatement = parent::prepareStatement("SELECT * FROM products WHERE product_id = ? and package_unit_id = ? and name = ? and localization_id = ?");
		$this->insertStatement = parent::prepareStatement("INSERT INTO products (package_unit_id, name) VALUES (?,?)");
		$this->listPackageTypeIdsStatement = parent::prepareStatement("select package_unit_id, concat( p.name, ' (', p.quantity,  m.name, ')') as package_unit  from packageunits p, metricsystemunits m where m.metric_system_unit_id = p.metric_system_unit_id");
	}
	
	function listPackageTypeIds(){
		$this->listPackageTypeIdsStatement->execute();
		$this->listPackageTypeIdsStatement->bind_result($package_unit_id, $package_unit);

		$resultSet = array();
		while ($this->listPackageTypeIdsStatement->fetch()) {
			//printf ("%s (%s)\n", $package_unit_id, $package_unit);
			array_push($resultSet, array($package_unit_id, $package_unit));
		}
		return $resultSet;
	}
	
	//TODO: move to sql code
	function insert($productId, $packageUnitId, $name, $localizationId = "", $natrium = "", $calories = ""){
		$rc = $this->insertStatement->bind_param("is", $packageUnitId, $name);
		if ( false===$rc ) {
            printf('bind_param failed: %s', htmlspecialchars($stmt->error));
			die;
        }
		$rc= $this->insertStatement->execute();
        /*if ( false===$rc ) {
                printf('execute failed: %s', htmlspecialchars($stmt->error));
                die;
        }
        $rc = $this->insertStatement->bind_result($productId, $packageUnitId, $name, $localizationId, $natrium, $calories);
        if ( false===$rc ) {
                printf('bind_result failed: %s', htmlspecialchars($stmt->error));
                die;
        }*/
		
		$this->insertStatement->fetch();
		
		return $rc;
	}
	
	function query(){
		return parent::query("select * from v_stocks");
	}
	
	function delete($product){
		echo "delete ". $product;
	}
}